<template>
  <div class="layout">
    <div class="layout-head">
      <template v-for="item in $route.matched[0].children">
        <div class="__hover" :class="{active: $route.matched[1].name == item.name}" @click="$router.push({name: item.name})">{{item.meta.title}}</div>
      </template>
    </div>
    <div class="layout-content">
      <div class="layout-content-menu">
        <div class="sub-menu-main">
          <template v-for="(item, index) in $route.matched[1].children">
            <div class="smm-parent">
              <div class="p-item __hover" :class="{current: item.name === $route.name || $route.matched[2].name === item.name}" @click="onParent(item)">
                {{item.meta.title}}
                <SvgIcon class="menu-expend" name="czr_arrow" size="12" color="#ffffff" :class="{active: item.expend}" v-if="item.children?.length > 0"/>
              </div>
              <div class="smm-son" v-if="item.children?.length > 0 && item.expend">
                <template v-for="(subItem, subIndex) in item.children">
                  <div class="s-item __hover" :class="{current: subItem.name === $route.name}" @click="toSubMenu(subItem)">
                    {{subItem.meta.title}}
                  </div>
                </template>
              </div>
            </div>
          </template>
        </div>
      </div>
      <div class="layout-content-main">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useRoute, useRouter} from "vue-router";
const route = useRoute()
const router = useRouter()
const state: any = reactive({})
const {proxy} = getCurrentInstance()

const toSubMenu = (menu: any) => {
  const deep = (r: any) => {
    if (r.children?.length > 0) {
      deep(r.children[0])
    } else {
      if (menu.name !== route.name) {
        router.push({
          name: r.name
        })
      }
    }
  }
  deep(menu)
}
const onParent = (item) => {
  if (item.children?.length > 0) {
    item.expend = !item.expend
    proxy.$forceUpdate()
  } else {
    toSubMenu(item)
  }
}
onMounted(() => {
  route.matched[1].children.forEach(v => {
    if (v.name == route.matched[2].name) {
      v.expend = true
      proxy.$forceUpdate()
    }
  })
})
</script>

<style lang="scss" scoped>
.layout {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .layout-head {
    background-image: url("@/assets/images/demo-head-bg.png");
    background-repeat: no-repeat;
    background-position-x: right;
    width: 100%;
    height: 59px;
    font-size: 30px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #FFFFFF;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 20px;
    .active {
      color: #22a5fe;
    }
  }
  .layout-content {
    flex: 1;
    display: flex;
    overflow: hidden;
    .layout-content-menu {
      width: 204px;
      height: 100%;
      .current {
        background-color: #0062E9;
      }
      .sub-menu-main {
        width: 100%;
        height: 100%;
        overflow-y: auto;
        background: linear-gradient(0deg, #021D78 0%, #0F44B6 100%);
        padding-top: 13px;
        .smm-parent {
          .p-item {
            display: flex;
            align-items: center;
            height: 44px;
            line-height: 1;
            user-select: none;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #FFFFFF;
            padding-left: 20px;
            .menu-logo {
              margin: 0 13px 0 15px;
            }
            .menu-expend {
              margin: 0 12px 0 auto;
              transition: 0.2s;
              &.active {
                transform: rotate(90deg) !important;
              }
            }
          }
          .smm-son {
            .s-item {
              height: 44px;
              display: flex;
              align-items: center;
              line-height: 1.2;
              user-select: none;
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #FFFFFF;
              padding-left: 40px;
            }
          }
        }
        .smm-parent-not-expend {
          height: 44px;
          display: flex;
          align-items: center;
          .menu-logo {
            margin: 0 13px 0 15px;
          }
        }
        :deep(.smm-parent-son) {
          background: #0a369f;
          border: none;
          padding: 0;
          width: auto !important;
          min-width: 0;
          .el-popper__arrow::before {
            background: #0a369f;
            border-color: #0a369f;
          }
          .smm-son {
            .s-item {
              padding: 0 20px;
              height: 44px;
              display: flex;
              align-items: center;
              line-height: 1.2;
              user-select: none;
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #FFFFFF;
            }
          }
        }
      }
    }
    .layout-content-main {
      flex: 1;
      overflow: hidden;
    }
  }
}
</style>
